import wx
from zope import interface
import os, sys
from amara import binderytools

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watch_builder.gui.watchobjects import interfaces as wa_ints


class ContextDelete(wx.Dialog):
    def __init__(self, parent, ID, title, size=wx.DefaultSize,
                 style=wx.DEFAULT_DIALOG_STYLE, child=None):
        wx.Dialog.__init__(self, parent, ID, title, size=size, style=style)
        # contents
        sizer = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(self, -1, "Are you sure?")
        sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        btnsizer = wx.StdDialogButtonSizer()
        
        btn = wx.Button(self, wx.ID_OK)
        btn.SetDefault()
        btnsizer.AddButton(btn)

        btn = wx.Button(self, wx.ID_CANCEL)
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        self.SetSizer(sizer)
        sizer.Fit(self)
        


class ContextEdit(wx.Dialog):
    def __init__(self, parent, ID, title="Default Edit Widget", size=wx.DefaultSize,
                 style=wx.DEFAULT_DIALOG_STYLE, child=None):

        pre = wx.PreDialog()
        pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
        pre.Create(parent, ID, title, size=size, style=style)
        self.PostCreate(pre)
        self.widgets = {}
        sizer = self.buildDialogForChild(child)
        # this is obviously a little open to error
        # if there is no sizer ready for child, it will break open
        # but we have absolute control of that, so we'll just make sure there *is* a dialog
        # for each child.
        self.SetSizer(sizer)
        sizer.Fit(self)


    def buildDialogForChild(self, child):
        interfaces_and_functions = [
            (wa_ints.IWatchDataObject, self.watchDialog), (wa_ints.IRootDataObject, self.rootDialog),
            (wa_ints.IBranchDataObject, self.branchDialog), (wa_ints.IExecutePathDataObject, self.executePathDialog),
        ]
        for int, func in interfaces_and_functions:
            if int.providedBy(child):
                return func(child)
        return None


    def nullDialog(self, child):
        sizer = wx.BoxSizer(wx.VERTICAL)
        
        label = wx.StaticText(self, -1, "There is nothing to edit for this element.")
        label.SetHelpText("Nothing to edit")
        sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)        
        
        btnsizer = wx.StdDialogButtonSizer()
        btn = wx.Button(self, wx.ID_OK)
        btn.SetHelpText("The OK button completes the dialog")
        btn.SetDefault()
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        return sizer

    
    def buildTextCtrl(self, attr, guiConf):
        box = None
        try:
            box = wx.BoxSizer(wx.HORIZONTAL)
            temp = guiConf.xml_xpath("//%s" % attr)[0]
            label = wx.StaticText(self, -1, str(temp.display_name))
            label.SetHelpText(str(temp.help_text))
            box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
            current_value = str(temp.current_value).strip()
            text = wx.TextCtrl(self, -1, current_value, size=(80,-1))
            self.widgets[attr] = text
            text.SetHelpText(str(temp.help_text))
            box.Add(text, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
            return box
        except IndexError:
            pass        
        return box

    
    def buildCheckbox(self, attr, guiConf):
        box = None
        try:
            box = wx.BoxSizer(wx.HORIZONTAL)
            temp = guiConf.xml_xpath("//%s" % attr)[0]
            current_value = str(temp.current_value).strip()
            if current_value.lower() == "true":
                current_value = True
            else:
                current_value = False
            cb = wx.CheckBox(self, -1, str(temp.display_name))
            cb.SetValue(current_value)
            self.widgets[attr] = cb
            cb.SetHelpText(str(temp.help_text))
            box.Add(cb, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
        except IndexError:
            pass
        return box
    

    def buildOkCancelHelp(self):
        btnsizer = wx.StdDialogButtonSizer()
        
        if wx.Platform != "__WXMSW__":
            btn = wx.ContextHelpButton(self)
            btnsizer.AddButton(btn)
        
        btn = wx.Button(self, wx.ID_OK)
        btn.SetHelpText("The OK button completes the dialog")
        btn.SetDefault()
        btnsizer.AddButton(btn)

        btn = wx.Button(self, wx.ID_CANCEL)
        btn.SetHelpText("The Cancel button cancels the dialog.")
        btnsizer.AddButton(btn)
        btnsizer.Realize()
        return btnsizer
    
    
    # below here, we begin to build the specific dialogs for different elements
    
    def watchDialog(self, child):
        self.widgets = {"startOnInit" : None, "final" : None, "rest" : None}
        sizer = wx.BoxSizer(wx.VERTICAL)

        label = wx.StaticText(self, -1, "Edit Watch Details")
        label.SetHelpText("Edit the details of the watch")
        sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        guiConf = binderytools.bind_string(child.guiObjXml())
        
        # start on init
        soi = self.buildCheckbox("startOnInit", guiConf)
        if soi:
            sizer.Add(soi, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        # set final
        final = self.buildCheckbox("final", guiConf)
        if final:
            sizer.Add(final, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        # set rest
        rest = self.buildTextCtrl("rest", guiConf)
        if rest:
            sizer.Add(rest, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
            
        # simple line divider
        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

        okcancelhelp = self.buildOkCancelHelp()

        sizer.Add(okcancelhelp, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        return sizer

    
    def branchDialog(self, child):
        return self.nullDialog(child)


    def executePathDialog(self, child):
        return self.nullDialog(child)
    
    
    def rootDialog(self, child):
        return self.nullDialog(child)